﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace SystemModule
{
    /// <summary>
    /// // 技能类
    /// </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    public class TMagic //Size 76
    {
        /// <summary>
        /// 技能ID
        /// </summary>
        public UInt16 wMagicId;
        /// <summary>
        /// 技能名称
        /// </summary>
        public string sMagicName;
        /// <summary>
        /// 动作效果
        /// </summary>
        public byte btEffectType;
        /// <summary>
        /// 魔法效果
        /// </summary>
        public byte btEffect;
        /// <summary>
        /// 未使用
        /// </summary>
        public byte bt11;
        /// <summary>
        /// 魔法消耗
        /// </summary>
        public UInt16 wSpell;
        /// <summary>
        /// 基本威力
        /// </summary>
        public UInt16 wPower;
        /// <summary>
        /// 技能等级
        /// </summary>
        public byte[] TrainLevel;//4
        /// <summary>
        ///  未使用 20080531
        /// </summary>
        public UInt16 w02;
        /// <summary>
        /// 各技能等级最高修炼点
        /// </summary>
        public int[] MaxTrain;//4
        /// <summary>
        /// 修炼等级
        /// </summary>
        public byte btTrainLv;
        /// <summary>
        /// 职业 0-战 1-法 2-道 3-刺客
        /// </summary>
        public byte btJob;
        /// <summary>
        ///  未使用
        /// </summary>
        public UInt16 wMagicIdx;
        /// <summary>
        /// 技能延时
        /// </summary>
        public uint dwDelayTime;
        /// <summary>
        /// 升级魔法
        /// </summary>
        public byte btDefSpell;
        /// <summary>
        /// 升级威力
        /// </summary>
        public byte btDefPower;
        /// <summary>
        /// 最大威力
        /// </summary>
        public UInt16 wMaxPower;
        /// <summary>
        /// 升级最大威力
        /// </summary>
        public byte btDefMaxPower;
        /// <summary>
        /// 技能描述
        /// </summary>
        public string sDescr;//18
        /// <summary>
        /// 技能类型（0：普通技能 1：英雄技能 2：内功技能）
        /// </summary>
        public byte btClass;

        public TMagic()
        {
            TrainLevel = new byte[4];
            MaxTrain = new int[4];
        }

        public byte[] ToByte()
        {
            using (var memoryStream = new MemoryStream())
            {
                var backingStream = new BinaryWriter(memoryStream);
                backingStream.Write(wMagicId);
                int NameLen = 0;
                byte[] NameBuff = HUtil32.StringToByteAry(sMagicName, out NameLen);
                NameBuff[0] = (byte)NameLen;
                Array.Resize(ref NameBuff, 13);
                backingStream.Write(NameBuff, 0, NameBuff.Length);
                backingStream.Write(btEffectType);
                backingStream.Write(btEffect);
                backingStream.Write(bt11);
                backingStream.Write(wSpell);
                backingStream.Write(wPower);
                backingStream.Write(TrainLevel);
                backingStream.Write(w02);
                backingStream.Write(MaxTrain[0]);
                backingStream.Write(MaxTrain[1]);
                backingStream.Write(MaxTrain[2]);
                backingStream.Write(MaxTrain[3]);
                backingStream.Write(btTrainLv);
                backingStream.Write(btJob);
                backingStream.Write(wMagicIdx);
                backingStream.Write(dwDelayTime);
                backingStream.Write(btDefSpell);
                backingStream.Write(btDefPower);
                backingStream.Write(wMaxPower);
                backingStream.Write(btDefMaxPower);
                int DescLen = 0;
                byte[] descBuff = HUtil32.StringToByteAry(sDescr, out DescLen);
                descBuff[0] = (byte)DescLen;
                Array.Resize(ref descBuff, 19);
                backingStream.Write(descBuff);
                return memoryStream.ToArray();
            }
        }
    }
}
